Skip to content

v4: absorb main 3.5.0, part 4 - pub/sub: retire subscriptions the server still holds - #3408

Merged
mgravell merged 1 commit into
v4from
marc/v4-absorb-3.5-b3
Dec 3, 2026
Merged

mgravell merged 1 commit into
v4from
marc/v4-absorb-3.5-b3

Conversation

@mgravell

@mgravell mgravell commented Dec 3, 2026

Copy link
Copy Markdown
Member

Fourth slice of absorbing main's 3.5.0 into v4 (after #3403, #3406, #3407): main's #3368, plus the part of #3353's tests that #3407 missed. #3387 follows separately.

The bug (#3367)

Unsubscribing a channel's last handler retires its subscription on the client whether or not the server-side unsubscribe succeeds. v4 had main's gap. Each of the following left the server subscribed, pushing messages nothing receives, with nothing on the client to clean it up until the connection was replaced:

  • the server declined the UNSUBSCRIBE (BUSY, LOADING, MASTERDOWN);
  • the UNSUBSCRIBE timed out;
  • the UNSUBSCRIBE was sent while the SUBSCRIBE was still unacknowledged.

How it maps onto v4

Main hooks into TrackSubscriptionsProcessor. Here the equivalent is the settle path in Subscription.SendAsync.

  • Retiring state. IsRetired is readable without the registration lock. A retry-needed flag is consumed before re-issuing, so an unsubscribe merely waiting on a slow reply is not re-sent on every heartbeat. Restore paths (EnsureSubscriptions, EnsureSubscribedToServer[Async], ResubscribeToServer) skip retired subscriptions.
  • Observed unsubscribes. The UNSUBSCRIBE is never sent as fire-and-forget. A caller who asked for that still doesn't wait, but a declined one is always observed. An incomplete one records the server, and the multiplexer tracks the retirement. It is re-issued from the heartbeat and from EnsureSubscriptions until confirmed or the connection drops, unless a newer subscription for the channel owns that server, or may yet claim it (TryUnsubscribeRetiringServer, under lock (subscriptions) as in main).
  • In-flight subscribe. An unsubscribe issued while the subscribe is unacknowledged goes out behind it, on the same connection. The caller therefore doesn't return with the server still subscribed, and the subscribe's ack doesn't send a duplicate. An ack for a retired subscription, or a stray ack for a live one (Duplicate Pub/Sub messages after Redis Sentinel failover #3336), is unsubscribed as a tracked retirement, so a declined cleanup is retried too.
  • Self-correction. An older, retired subscription's UNSUBSCRIBE can land on a server after a newer subscription for the channel was restored there. When that happens, the newer one forgets the server and subscribes again.
  • Synchronous Unsubscribe surfaces the server's RedisServerException (Busy), as the async path does, rather than an AggregateException from the wait.

One difference from main: v4 completes settle continuations on the thread pool, so the reply to an UNSUBSCRIBE can be handled after the reply to a later SUBSCRIBE on the same connection. The self-correction can then re-subscribe a subscription that was fine. That costs one extra, idempotent SUBSCRIBE, and no messages are lost.

From main's SubscriptionRestorationTests (#3353)

#3353 created this file, and #3407 ported only SubscriptionRestoreRaceTests. Bringing it across exposed three more things:

  • A timed-out subscribe's late ack reaches nobody in v4, so its registration was never cleaned up (LateAcknowledgementCannotReplaceANewerSubscription). A timeout is the one failure that leaves the server's state unknown. That server is now tracked as a possible stray: the record is dropped if the subscription comes back to it, and the server is unsubscribed if the subscription lands elsewhere or retires.
  • An internal restore that deferred to an in-flight attempt is now re-driven when that attempt fails (DeferredRestoreIsRedrivenWhenTheInFlightAttemptFails). Previously nothing brought it back.
  • UnsubscribeAsync's task carries the subscriber's async state (UnsubscribeTaskPreservesTheCallersAsyncState). This was an existing v4 bug.

Tests

  • FailedUnsubscribeUnitTests (18 cases): 14 failed on v4 before this; all pass now. One send was replaced with v4's Subscription.SendAsync.
  • SubscriptionRestorationTests (44 cases): the old-core sends are replaced by v4's:
    • a ping on the subscription socket as a barrier;
    • PUBLISH pinned via Connections.ServerContext;
    • Subscription.SendAsync for the stray.
  • The bridge-identity test keeps its intent (after the subscription connection drops, the subscription is restored on the new one and delivers). It waits for the ConnectionFailed event instead of comparing PhysicalBridge instances, which v4 doesn't have.
  • Wider suites: pub/sub, sharded, RESP3, failover, sentinel, maintenance, endpoint pruning and SanityChecks pass: net10.0 774 (108 skipped), net481 770 (107 skipped). The four subscription race classes pass three runs in a row.

… restore and the heartbeat (port of main #3368)

Unsubscribing a channel's last handler retires its subscription on the client whether or not the
server-side unsubscribe succeeds. v4 had main's gap: a declined (BUSY, LOADING, MASTERDOWN) or
timed-out UNSUBSCRIBE, or one sent while the SUBSCRIBE was still unacknowledged, left the server
subscribed with nothing left to clean it up until the connection was replaced.

- Subscription: a retiring state readable without the registration lock, plus a retry-needed flag
  consumed before re-issuing, so a slow reply is not re-sent on every heartbeat. Restore paths skip
  retired subscriptions.
- The UNSUBSCRIBE is never sent as fire-and-forget, though a caller who asked for that still does
  not wait, so a declined one is always observed. An incomplete one records the server and is
  tracked by the multiplexer, which re-issues it from the heartbeat and from EnsureSubscriptions
  unless a newer subscription owns, or may yet claim, that server.
- An unsubscribe while the subscribe is in flight goes out behind it on the same connection, so
  the caller does not return with the server still subscribed. An ack for a retired subscription,
  or a stray for a live one, is unsubscribed as a tracked retirement.
- Self-correction: an older UNSUBSCRIBE that lands after a newer subscription for the channel was
  restored onto the same server re-subscribes that one.
- The synchronous Unsubscribe surfaces the server's exception rather than an AggregateException.

Also from main's SubscriptionRestorationTests, which #3353 created and #3407 missed:
- a timed-out subscribe's server is tracked as a possible stray, since its late ack reaches nobody
  in v4;
- an internal restore that deferred to an in-flight attempt is re-driven when that attempt fails;
- UnsubscribeAsync's task carries the subscriber's async state (an existing v4 bug).

Main's FailedUnsubscribeUnitTests (14 of 18 failed on v4) and SubscriptionRestorationTests come
across with old-core sends replaced by v4's; the bridge-identity test keeps its intent (restored
on the new connection, and delivers) without the PhysicalBridge assertions.
@mgravell
mgravell merged commit 6017349 into v4 Dec 3, 2026
3 checks passed
@mgravell
mgravell deleted the marc/v4-absorb-3.5-b3 branch December 3, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant